/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.instrument.elementui;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;

import twaver.TWaverUtil;
import twaver.Node;
import twaver.network.TNetwork;
import twaver.network.ui.NodeUI;

public class StateUI extends NodeUI {
    public StateUI(TNetwork network, final Node node) {
        super(network, node);
    }

    public void paintBody(Graphics2D g2) {
        super.paintBody(g2);
        
        Boolean state = (Boolean)this.getElement().getClientProperty("equipment.state");
        if (state != null && state.booleanValue()) {
        	Rectangle bounds = element.getBounds();
        	GradientPaint gp = new GradientPaint(
        			bounds.x, bounds.y, new Color(0, 255, 0, 180), 
        			bounds.x + bounds.width, bounds.y + bounds.height, new Color(255, 255, 255, 180), true);            
            g2.setPaint(gp);
            Shape shape = TWaverUtil.getImageShape(getNode().getImageURL());
            Point location = getNode().getLocation();
            AffineTransform at = AffineTransform.getTranslateInstance(location.getX(), location.getY());
            shape = at.createTransformedShape(shape);
            g2.fill(shape);
        } else {
            Integer value = (Integer)this.getElement().getClientProperty("equipment.value");
            if (value != null) {
                if (value.intValue() > 15) {
                    g2.setColor(Color.RED);
                } else {
                    g2.setColor(Color.GREEN);
                }
                Point p = this.getElement().getLocation();
                double r = 5;
                double y = 61;
                double x = 46;
                double xOffset = 10;
                double yOffset = -2.6;
                g2.fill(new Ellipse2D.Double(p.x + x, p.y + y, r, r));
                g2.fill(new Ellipse2D.Double(p.x + x + 1 * xOffset, p.y + y + 1 * yOffset, r, r));
                g2.fill(new Ellipse2D.Double(p.x + x + 2 * xOffset, p.y + y + 2 * yOffset, r, r));
                g2.fill(new Ellipse2D.Double(p.x + x + 3 * xOffset, p.y + y + 3 * yOffset, r, r));
                g2.fill(new Ellipse2D.Double(p.x + x + 4 * xOffset, p.y + y + 4 * yOffset, r, r));
                g2.fill(new Ellipse2D.Double(p.x + x + 5 * xOffset, p.y + y + 5 * yOffset, r, r));
            }
        }
    }
}